# coding=utf8
from typing import TypeVar
from struct import unpack

u1 = TypeVar('u1')
u2 = TypeVar('u2')
u4 = TypeVar('u4')

'''
x	        pad byte(填充字节)	no value	 
c	        char	            string of length 1	    1
b	        signed char	        integer	                1
B	        unsigned char	    integer	                1
?	        _Bool	            bool	                1
h	        short	            integer	                2
H	        unsigned short	    integer	                2
i	        int	                integer	                4
I(大写的i)	unsigned int	    integer	                4
l(小写的L)	long	            integer	                4
L	        unsigned long	    long	                4
q	        long long	        long	                8
Q	        unsigned long long	long	                8
f	        float	            float	                4
d	        double	            float	                8
s	        char[]	            string	 
p	        char[]	            string	 
P	        void *	            long
——————————————————————————————
'''


def __convert_def(raw: str):
    def unpack_to_val(buffer: bytes) -> any:
        return unpack(raw, buffer)[0]

    return unpack_to_val


int8 = __convert_def('>b')
int16 = __convert_def('>h')
int32 = __convert_def('>i')
int64 = __convert_def('>q')

uint8 = __convert_def('>B')
uint16 = __convert_def('>H')
uint32 = __convert_def('>I')
uint64 = __convert_def('>Q')

float0 = __convert_def('>f')
double = __convert_def('>d')

char = __convert_def('>c')


def utf8(buffer):
    return buffer.decode('utf8')


__all__ = ['u1', 'u2', 'u4', 'int8', 'int16', 'int32', 'int64', 'utf8', 'uint8', 'uint16',
           'uint32', 'uint64', 'float0', 'double', 'char']
